/**
 * @file 预约时间
 * @author yangjiangming <449654337@qq.com>
 * @since 2018/12/14 16:38:57
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laytpl'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laytpl = layui.laytpl;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-140'
    });
    laypage.render(layui.$.extend({limits:[50,100,150]},zmnPage.laypageOptions(json)));
    spConfig.laySelectCity(form, "/plat/sp/config/listServCategBySpIdAndCity.action", "cityId", "servCategId");
    spConfig.laySelectSpecial(form, "/plat/sp/config/listCategOneByManageCompanyIdAndServCategIdAndShowTypeAndCityId.action", "/plat/sp/config/listCategTwoByManageCompanyIdAndServAndCategOneIdAndShowTypeAndCityId.action", "servCategId", "categOneId", "categTwoId");


    $("#reset").on("click", function () {
        // $('#cityId').val('');
        // $('#servCategId').val('');
        // $('#categOneId').val('');
        // $('#categTwoId').val('');
        $("#cityId option").attr("selected", false);
        $("#servCategId option").attr("selected", false);
        $("#categOneId option").attr("selected", false);
        $("#categTwoId option").attr("selected", false);
        form.render('select')
    });

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.configId);
    });

    table.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "batchSet") {
            batchSet(json.spId);
        }
    });

    //【表格右侧工具栏监听】
    table.on('tool(zmn-table)', function (obj) {
        if (obj.event === 'updateStock') {
            laytpl(updateStockHtml.innerHTML).render(obj.data, function(html){
                childIndex = layer.open({
                    type: 1,
                    title: '修改库存'
                    ,content: html
                    ,area: ['360px', '220px']
                });
            });
        }
    });

    // 提交
    form.on('submit(zmn-form-stock-submit)', function(obj){
        var data = obj.field;
        var url = "/plat/sp/warning/config/updateStock.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            dataType: "json", data: data,
            success:function(resp) {
                if(resp.status == 200) {
                    zmnPage.reload('操作成功!', 1000);
                }else{
                    layer.msg(resp.message,{icon: 2});
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });

    // 修改节假日系数
    table.on("edit(zmn-table)", function (obj) {
        let verify = function(value){
            if (value == null || value == undefined || value == ''){
                return;
            }
            var reg = new RegExp("^(([1-9][0-9]*)|(([0]\\.\\d{1,1}|[1-9][0-9]*\\.\\d{1,1})))$");
            if(!reg.test(value)){
                return "请输入正确的格式!最多保留一位小数";
            }
            if(parseFloat(value) < 0.6 || parseFloat(value) > 3){
                return "系数值不符合规定,系数值范围0.6~3.0";
            }
        }
        let msg = verify(obj.value);
        if(zmnUtil.isNotNull(msg)){
            layer.alert(msg, {icon: 2}, function(index){
                location.reload();
            });
            return;
        }

        $.ajax({
            async: false,
            type: "POST", url: "/plat/sp/warning/config/updateCoefficient.action",
            dataType: "json", data: { configId: obj.data.configId, value: obj.value },
            success:function(resp) {
                if(resp.status == 200) {
                    zmnPage.reload('操作成功!', 1000);
                }else{
                    layer.msg(resp.message,{icon: 2});
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
    });

    exports("spwarning_list_js");
});

function batchSet(spId){
    zmnLayUtil.openIframeCustom(layer, "/plat/sp/warning/config/batchSet.action?spId="+spId, {title: "批量设置", shade: 0.9, area:["65%", "80%"], end: function(){location.reload()}});
}


var spConfig = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, url2, selectOneId, selectTwoId, selectThrId, selectTwoOperator) {
        let $ = layui.$;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            let servCategId = obj.value;
            let cityId = $("#cityId").val()
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {servCategId: servCategId, cityId: cityId, spId: json.spId},
                success: function (result) {
                    let items = result.data;
                    if (items == null) {
                        $("#" + selectTwoId).html("");
                        $("#" + selectThrId).html("");
                        form.render("select");
                        return;
                    }
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i].value + '">' + items[i].text + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    $("#" + selectThrId).html("");
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        ;

        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            let categOneId = obj.value;
            let servCategId = $('#servCategId').val();
            let cityId = $("#cityId").val();
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {servCategId: servCategId, categOneId: categOneId, cityId: cityId, spId: json.spId},
                success: function (result) {
                    let items = result.data;
                    let defaultOperator = function(){
                        if (items == null) {
                            $("#" + selectThrId).html("");
                            form.render("select");
                            return;
                        }
                        var optionHtml = '<option value="0">选择</option>';
                        for (var i = 0; i < items.length; i++) {
                            optionHtml += '<option value="' + items[i].value + '">' + items[i].text + '</option>';
                        }
                        $("#" + selectThrId).html(optionHtml);
                        form.render("select");
                    }
                    if(selectTwoOperator){
                        selectTwoOperator(items);
                    }else{
                        defaultOperator();
                    }
                }
            });
        });
    },
    laySelectCity: function (form, url, selectOneId, selectTwoId) {
        let $ = layui.$;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            let cityId = obj.value;
            $.ajax({
                type: "GET", url: url,
                dataType: "json", data: {cityId: cityId, spId: json.spId},
                success: function (result) {
                    let items = result.data;
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i].categId + '">' + items[i].name + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    $("#categOneId").html("");
                    $("#categId").html("");
                    form.render("select");
                }
            });
        });
    }
}
